﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using FrameworkTemplate.Windows.Commands;
using FrameworkTemplate.Workers;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using Script.ClearScript_V8;
using User;
using Utils.Log;

namespace FrameworkTemplate.Windows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        MainWindowViewModel viewModel;
        bool close_confirm = false;
        public MainWindow()
        {
            viewModel = new MainWindowViewModel();
            DataContext = viewModel;
            InitializeComponent();
            logControl.BindLogWorker(Log.logWorker);
            //logControl.DataContext = Log.logWorker;

            LoadLanguageMenu();
            ReloadLanguage(Properties.Settings.Default.当前语言);

        }
        private void MetroWindow_Closed(object sender, EventArgs e)
        {
            var windows = (DataContext as MainWindowViewModel).windows;
            foreach (var window in windows.Values.ToArray())
            {
                if (window != null)
                    window.Close();
            }
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            user_menu_name.SetBinding(Label.ContentProperty, new Binding { Source = GLOBAL.user, Path = new PropertyPath("name") });
            user_name.SetBinding(Label.ContentProperty, new Binding { Source = GLOBAL.user, Path = new PropertyPath("name") });
            user_right.SetBinding(Label.ContentProperty, new Binding { Source = GLOBAL.user, Path = new PropertyPath("right") });

            GLOBAL.Init();
        }

        /// <summary>
        /// 载入语言并添加到语言菜单选项
        /// </summary>
        private void LoadLanguageMenu()
        {
            language.Items.Clear();
            var cultures = GLOBAL.GetRegisterCultures();
            foreach (var c in cultures)
            {
                var mi = new MenuItem()
                {
                    IsCheckable = true,
                    Header = c.DisplayName,
                    DataContext = c
                };
                mi.Click += Language_menuitem_click;
                language.Items.Add(mi);
            }
        }

        /// <summary>
        /// 重新载入语言
        /// </summary>
        /// <param name="language_name"></param>
        private void ReloadLanguage(string language_name)
        {
            GLOBAL.LoadLanguage(GLOBAL.GetCulture(language_name));
            var cultures = GLOBAL.GetRegisterCultures();
            foreach(dynamic mi in language.Items)
            {
                //var mi = m as MenuItem;
                if (mi.DataContext.Name == language_name)
                    mi.IsChecked = true;
                else
                    mi.IsChecked = false;
            }
        }

        /// <summary>
        /// 语言选项点击语言时触发的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Language_menuitem_click(object sender, RoutedEventArgs e)
        {
            var mi = sender as MenuItem;
            if (mi.IsChecked)
            {
                GLOBAL.SetLanguage(mi.DataContext.ToString());
                ReloadLanguage(mi.DataContext.ToString());
            }
            else
                mi.IsChecked = true;

        }

        /// <summary>
        /// 无需确认直接关闭窗口
        /// </summary>
        public void DirectClose()
        {
            close_confirm = true;
            this.Close();
        }
        private async void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!close_confirm)
                e.Cancel = true;

            var result = await this.ShowMessageAsync(GLOBAL.Resources("quit_app"),
                                                     GLOBAL.Resources("sure_quit_app"),
                                                     MessageDialogStyle.AffirmativeAndNegative);
           var close = result == MessageDialogResult.Affirmative;
            if (close)
                DirectClose();

        }

        private void User_button_Click(object sender, RoutedEventArgs e)
        {
            user_popup.HorizontalOffset = this.ActualWidth - popup_grid.Width;
            user_popup.IsOpen = false;
            user_popup.IsOpen = true;
        }
    }
}
